<template>
  <div class="execution">
    <basic-container>
      <el-radio-group v-model="params.status" style="margin-bottom: 15px;">
        <el-radio-button label="0">待阅</el-radio-button>
        <el-radio-button label="1">已阅</el-radio-button>
      </el-radio-group>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList">
        <template slot-scope="scope" slot="menuLeft">
          <el-button type="primary"
                     size="small"
                     v-if="params.status=='0'"
                     @click.stop="handleBatchReview()">批量审阅
          </el-button>
        </template>
        <template slot="menu" slot-scope="scope">
          <el-button type="text" size="small"
            @click="handleView(scope.row)">查 看</el-button>
          <el-button type="text" size="small" v-if="scope.row.status == 1"
                     @click="unread(scope.row)">撤回已阅</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import baseCrud from '@/mixins/base-crud'
import {review, batchReview, recallUnread} from '@/api/support/swmessage'

export default {
  name: 'swmessage',
  mixins: [baseCrud('support/swmessage')],
  data() {
    return {
      params: {
        status: '0',
        searchType: ''
      }
    }
  },
  computed: {
    permissionList() {
      return {}
    }
  },
  watch: {
    'params.status'(val){
      // 已阅页面显示已阅时间
      this.findObject(this.tableOption.column, 'readTime').hide = (val == '0')
      this.findObject(this.tableOption.column, 'readTime').search = (val != '0')
      // 直接改列的hide属性不生效，需要更新column触发页面刷新
      this.$set(this.tableOption, 'column', this.deepClone(this.tableOption.column))
      this.params.searchType = '1'
      this.getList(this.page)
    }
  },
  methods: {
    handleBatchReview(){
      var tableSelect = this.$refs.crud.tableSelect
      if(tableSelect.length == 0){
        this.$message.info('请选择要审阅的记录')
        return
      }
      var ids = tableSelect.map(row => row.id)
      batchReview(ids).then(response => {
        this.$message.success('审阅成功')
        this.getList(this.page)
      })
    },
    unread(row){
      if (row.status == '1'){
        recallUnread(row.id).then(response => {
          this.getList(this.page)
        })
      }
    },
    handleView(row){
      if(row.status == '0'){
        review(row.id).then(response => {
          this.$router.push(row.url)
          this.getList(this.page)
        })
      }else{
        this.$router.push(row.url)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
